# -*- coding: ISO-8859-1 -*-                                                                                                                                

from Tkinter import *


class DialogOk( Toplevel ):

    '''Class to open dialogs.

    This class is intended as a base class for custom dialogs
    '''

    def __init__( self, vue, title, message ):
        '''Initialize a dialog.

        Arguments:

            parent -- a parent window (the application window)

            title -- the dialog title
        '''
        self.v = vue
        self.message = message
        Toplevel.__init__( self, vue.root )
        # If the master is not viewable, don't
        # make the child transient, or else it
        # would be opened withdrawn
        if vue.root.winfo_viewable():
            self.transient( vue.root )

        if title:
            self.title( title )

        self.result = None

        body = Frame( self )
        self.initial_focus = self.body( body )
        body.pack( padx = 5, pady = 5 )
        self.buttonbox()
        self.wait_visibility() # window needs to be visible for the grab
        self.overrideredirect( 1 )
        self.grab_set()

        if not self.initial_focus:
            self.initial_focus = self

        self.protocol( "WM_DELETE_WINDOW", self.cancel )

        if self.v.root is not None:
            self.geometry( "+%d+%d" % ( self.v.root.winfo_rootx() + 300,
                                      self.v.root.winfo_rooty() + 300 ) )

        self.initial_focus.focus_set()

        self.wait_window( self )

    def destroy( self ):
        '''Destroy the window'''
        self.initial_focus = None
        Toplevel.destroy( self )

    #
    # construction hooks

    def body( self, master ):
        '''create dialog body.

        return widget that should have initial focus.
        This method should be overridden, and is called
        by the __init__ method.
        '''
        self.configure( bg = self.v.bgDialog )
        self.l_Message = Label( self, fg = self.v.fgUnselect, bg = self.v.bgDialog, font = self.v.font3, text = self.message )
        self.l_Message.pack()

    def buttonbox( self ):
        '''add standard button box.

        override if you do not want the standard buttons
        '''

        box = Frame( self, bg = self.v.bgDialog )
        self.b_ok = Button( box, fg = self.v.fgUnselect, bg = self.v.bgDialog, font = self.v.font3, text = "Ok", command = self.ok, width = 10, default = ACTIVE, activebackground = self.v.bgActive )
        self.b_ok.pack( side = LEFT, padx = 5, pady = 5 )
        #self.b_cancel = Button( box, fg = self.v.fgUnselect, bg = self.v.bgDialog, font = self.v.font3, text = "Cancel", width = 10, command = self.cancel )
        #self.b_cancel.pack( side = LEFT, padx = 5, pady = 5 )

        self.bind( "<Return>", self.ok )
        self.bind( "<Escape>", self.cancel )

        box.pack()

    #
    # standard button semantics

    def ok( self, event = None ):

        if not self.validate():
            self.initial_focus.focus_set() # put focus back
            return

        self.withdraw()
        self.update_idletasks()

        try:
            self.apply()
        finally:
            self.cancel()

    def cancel( self, event = None ):

        # put focus back to the vue.root window
        if self.v.root is not None:
            self.v.root.focus_set()
        self.destroy()

    #
    # command hooks

    def validate( self ):
        '''validate the data

        This method is called automatically to validate the data before the
        dialog is destroyed. By default, it always validates OK.
        '''

        return 1 # override

    def apply( self ):
        '''process the data

        This method is called automatically to process the data, *after*
        the dialog is destroyed. By default, it does nothing.
        '''
        pass


class InputDialog( Toplevel ):

    '''Class to open dialogs.

    This class is intended as a base class for custom dialogs
    '''

    def __init__( self, vue, title, question ):
        '''Initialize a dialog.

        Arguments:

            parent -- a parent window (the application window)

            title -- the dialog title
        '''
        self.v = vue
        self.question = question
        Toplevel.__init__( self, vue.root )
        # If the master is not viewable, don't
        # make the child transient, or else it
        # would be opened withdrawn
        self.transient( vue.root )

        if title:
            self.title( title )
        #Changement ici:
        self.result = ""

        body = Frame( self )
        self.initial_focus = self.body( body )
        body.pack( padx = 5, pady = 5 )
        self.buttonbox()
        self.wait_visibility() # window needs to be visible for the grab
        self.overrideredirect( 1 )
        self.grab_set()

        if not self.initial_focus:
            self.initial_focus = self

        self.protocol( "WM_DELETE_WINDOW", self.cancel )

        if self.v.root is not None:
            self.geometry( "+%d+%d" % ( self.v.root.winfo_rootx() + 300,
                                      self.v.root.winfo_rooty() + 300 ) )

        self.initial_focus.focus_set()

        self.wait_window( self )

    def destroy( self ):
        '''Destroy the window'''
        self.initial_focus = None
        Toplevel.destroy( self )

    #
    # construction hooks

    def body( self, master ):
        '''create dialog body.

        return widget that should have initial focus.
        This method should be overridden, and is called
        by the __init__ method.
        '''
        self.configure( bg = self.v.bgDialog )
        self.l_Question = Label( self, fg = self.v.fgUnselect, bg = self.v.bgDialog, font = self.v.font3, text = self.question )
        self.e_Input = Entry( self, fg = self.v.fgUnselect, bg = self.v.bgDialog, font = self.v.font3 )
        self.l_Question.pack()
        self.e_Input.pack()
        return self.e_Input

    def buttonbox( self ):
        '''add standard button box.

        override if you do not want the standard buttons
        '''

        box = Frame( self, bg = self.v.bgDialog )
        self.b_ok = Button( box, fg = self.v.fgUnselect, bg = self.v.bgDialog, font = self.v.font3, text = "Ok", command = self.ok, width = 10, default = ACTIVE , activebackground = self.v.bgActive )
        self.b_ok.pack( side = LEFT, padx = 5, pady = 5 )
        self.b_cancel = Button( box, fg = self.v.fgUnselect, bg = self.v.bgDialog, font = self.v.font3, text = "Cancel", width = 10, command = self.cancel, activebackground = self.v.bgActive )
        self.b_cancel.pack( side = LEFT, padx = 5, pady = 5 )

        self.bind( "<Return>", self.ok )
        self.bind( "<Escape>", self.cancel )

        box.pack()

    #
    # standard button semantics

    def ok( self, event = None ):

        if not self.validate():
            self.initial_focus.focus_set() # put focus back
            return

        self.withdraw()
        self.update_idletasks()

        try:
            self.apply()
        finally:
            self.cancel()

    def cancel( self, event = None ):

        # put focus back to the vue.root window
        if self.v.root is not None:
            self.v.root.focus_set()
        self.destroy()

    #
    # command hooks

    def validate( self ):
        '''validate the data

        This method is called automatically to validate the data before the
        dialog is destroyed. By default, it always validates OK.
        '''

        return 1 # override

    def apply( self ):
        '''process the data

        This method is called automatically to process the data, *after*
        the dialog is destroyed. By default, it does nothing.
        '''
        rep = self.e_Input.get()
        self.result = rep


